﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.PhotoRindGrid;
using MINERP.MODEL.PhotoRindGrid;
using MINERP.COM;

namespace MINERP.PhotoRindGrid.Forms
{
    public partial class FrmReCheck : Form
    {
        public FrmReCheck()
        {
            InitializeComponent();
        }
        PhotoRindGridBLL2 _bll = new PhotoRindGridBLL2();
        List<PhotoView> listphoto = new List<PhotoView>();
        private void textBoxPhotoBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strPhBarcode = textBoxPhotoBarCode.Text.Trim();

                if (strPhBarcode.Trim().Length == 0)
                {
                    return;
                }

                if (strPhBarcode.Length != 10)
                {
                    strPhBarcode = "3" + string.Format("{0:d9}", Convert.ToInt32(strPhBarcode));
                }
                textBoxPhotoBarCode.Text = strPhBarcode;
                //int orderproductid = Convert.ToInt32(strPhBarcode);
                if (listphoto.Count==0)
                {
                    listphoto = _bll.GetPhotoListByOnePhotoBarcode(strPhBarcode);
                    if (listphoto == null)
                    {
                        MessageBox.Show("照片不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    SavePhotoStatus(strPhBarcode);
                    gridControlPhoto.DataSource = listphoto;
                    PhotoGridOrderHeadInfo oihead = _bll.GetHeadInfoStr(strPhBarcode);
                   DisplayHeaderInfo(oihead, listphoto.Count, listphoto.Where(p=>p.isCheck==true).Count());
                }
                else
                {
                    SavePhotoStatus(strPhBarcode);
                }
                this.textBoxPhotoBarCode.Text = "";
                
 
            }
        }
        void DisplayHeaderInfo(PhotoGridOrderHeadInfo inHeadInfo,int inNum,int inNewNum)
        {
            textBoxShopName.Text = inHeadInfo.shopName;
            textBoxAllNum.Text = inNum.ToString();
            textBoxCtName.Text = inHeadInfo.ctmName;
            textBoxNowNum.Text = inNewNum.ToString();
            textBoxProductName.Text = inHeadInfo.orderProductName;
            textBoxProductBarCode.Text = inHeadInfo.orderInfoCode;
            textBoxBagBarCode.Text = inHeadInfo.orderBagCode;
            if (inNum == inNewNum)
            {
                listphoto = new List<PhotoView>();
                MessageBox.Show("该产品检查完毕！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        void SavePhotoStatus(string inStrBarCode)
        {
            if (listphoto.Count == 0)
            {
                MessageBox.Show("照片不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            PhotoView view = listphoto.FirstOrDefault(p => p.PhotoBarCode == inStrBarCode);
            if (view == null)
            {
                MessageBox.Show("该照片不是此产品的照片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                view.isCheck = true;
                gridControlPhoto.RefreshDataSource();
            }
            textBoxNowNum.Text = listphoto.Where(p => p.isCheck == true).Count().ToString();
        }

        private void gridViewphoto_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >=0)
            {
                PhotoView view = (PhotoView)gridViewphoto.GetRow(e.RowHandle);
                if (view.isCheck == true)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxShopName.Text ="";
            textBoxAllNum.Text = "";
            textBoxCtName.Text = "";
            textBoxNowNum.Text = "";
            textBoxProductName.Text = "";
            textBoxProductBarCode.Text = "";
            textBoxBagBarCode.Text = "";
            listphoto = new List<PhotoView>();
            gridControlPhoto.DataSource = listphoto;
        }

        private void gridViewphoto_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "IsCheck")
                {
                    PhotoView view = (PhotoView)gridViewphoto.GetRow(e.RowHandle);
                    if (view.isCheck == true)
                    {
                        e.DisplayText = "是";
                    }
                    else
                    {
                        e.DisplayText = "否";
                    }
                }
                if (e.Column.FieldName == "Status")
                {
                    int status =

                        Convert.ToInt32(
                            ((PhotoView)(gridViewphoto.GetRow(e.RowHandle))).Status);
                    switch (status)
                    {
                        case (int)PhotoStatus.NoDistributePrinter:
                            {

                                e.DisplayText = "未分配打印机";
                            }
                            break;
                        case (int)PhotoStatus.DistributedPrinter:
                            {

                                e.DisplayText = "等待打印";
                            }
                            break;
                        case (int)PhotoStatus.PingBan:
                            {

                                e.DisplayText = "已拼版,未打印";
                            }
                            break;
                        case (int)PhotoStatus.Printed:
                            {
                                e.DisplayText = "等待分片";
                            }
                            break;
                        case (int)PhotoStatus.FenPian:
                            {
                                e.DisplayText = "分片完成";
                            }
                            break;
                        case (int)PhotoStatus.PhotoInnerReturn:
                            {
                                e.DisplayText = "照片返单中";
                            }
                            break;
                        case (int)PhotoStatus.PingBanInd:
                            {
                                e.DisplayText = "拼版中";
                            }
                            break;
                        case (int)PhotoStatus.WaitPhotoMove:
                            {
                                e.DisplayText = "照片等待移动";
                            }
                            break;
                        case (int)PhotoStatus.PhotoNotExist:
                            {
                                e.DisplayText = "照片不存在";
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private void FrmReCheck_Load(object sender, EventArgs e)
        {
            this.textBoxPhotoBarCode.Focus();
        }

       

        

      
    }
}
